package com.it.selfAfter;

/**
 * @Author: CaoYouGen
 * @DateTime: 2023/05/22/13:05
 * @注释: TODO
 **/
import java.io.FileInputStream;
import java.security.Key;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.util.Enumeration;

public class KeyStoreViewe {
    public static void main(String[] args) {
        String keystoreFile = "src/main/resources/keystore.jks"; // 密钥库文件路径
        String keystorePassword = "www.Coke_Anne.com"; // 密钥库密码

        try {
            KeyStore keystore = KeyStore.getInstance("JKS");
            FileInputStream fis = new FileInputStream(keystoreFile);
            keystore.load(fis, keystorePassword.toCharArray());
            Enumeration<String> aliases = keystore.aliases();
            // 遍历密钥库中的条目
            while (aliases.hasMoreElements()){
                String alias = aliases.nextElement();
                if (keystore.isKeyEntry(alias)) {
                    Key key = keystore.getKey(alias, keystorePassword.toCharArray());
                    Certificate cert = keystore.getCertificate(alias);

                    System.out.println("Alias: " + alias);
                    System.out.println("Public Key: " + cert.getPublicKey());
                    System.out.println("Private Key: " + key);
                    System.out.println();
                }
            }
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
